import os
import shutil
import sys


def syncdir(source_folder, target_folder):
    
    if not os.path.exists(target_folder):
        os.mkdir(target_folder)

    for file in os.listdir(source_folder):
        
        from_file = os.path.join(source_folder, file)
        to_file = os.path.join(target_folder, file)
        print(to_file);
        if os.path.isdir(from_file):
            syncdir(from_file, to_file)
        else: 
            if iscopy(from_file, to_file):
                shutil.copy2(from_file, target_folder)
                print("copy %s from %s to %s;" % (file , from_file , to_file))
            else:
                print("The file %s is exist" % to_file)


def iscopy(from_file, to_file):
    # if(not os.path.exists(to_file)): # 目标文件还不存在，当然要拷过去啦
    #     return True
    #
    # from_file_modify_time = round(os.stat(from_file).st_mtime, 1) # 这里精确度为0.1秒
    # to_file_modify_time = round(os.stat(to_file).st_mtime, 1) # 拿到　两边文件的最后修改时间
    # if(from_file_modify_time > to_file_modify_time): # 比较　两边文件的　最后修改时间
    #     return True
    #
    return True

if __name__ == '__main__':
    express = {
        "E:\\SVN\\Repository\\前台\\mmsweb\\truck\\frontweb\\dms\\index.html": [
            "E:\\SVN\\Repository\\后台\\KisenIntelli\\mms\\mms-web\\src\\main\\webapp",
            "E:\\SVN\\Repository\\后台\\KisenIntelli\\mms\\mms-web-view\\src\\main\\webapp"
        ],
        "E:\\SVN\\Repository\\前台\\mmsweb\\truck\\frontweb\\dms\\dist": [
            "E:\\SVN\\Repository\\后台\\KisenIntelli\\mms\\mms-web\\src\\main\\webapp\\dist",
            "E:\\SVN\\Repository\\后台\\KisenIntelli\\mms\\mms-web-view\\src\\main\\webapp\\dist"
        ],
        "E:\\SVN\\Repository\\前台\\mmsweb\\truck\\frontweb\\dms\\lib": [
            "E:\\SVN\\Repository\\后台\\KisenIntelli\\mms\\mms-web\\src\\main\\webapp\\lib",
            "E:\\SVN\\Repository\\后台\\KisenIntelli\\mms\\mms-web-view\\src\\main\\webapp\\lib"
        ]
    }

    for source, targets in express.items():
        if os.path.exists(source):
            for target in targets:
                if os.path.isfile(source):
                    shutil.copy2(source, target)
                    print("copy %s to %s;" % (source, target))
                else:
                    shutil.rmtree(target)
                    syncdir(source,target)